package com.tianji.learning.mq;


import com.tianji.api.dto.remark.LikedTimesDTO;
import com.tianji.common.constants.MqConstants;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.service.IInteractionReplyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

//@Component
@Slf4j
@RequiredArgsConstructor
public class LikeTimesChangeListener1 {
    private final IInteractionReplyService replyService;

    //交换机，路由器，key
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "qa.liked.times.queue1", durable = "true"),
                    exchange = @Exchange(value = MqConstants.Exchange.LIKE_RECORD_EXCHANGE, type = ExchangeTypes.TOPIC),
                    key = MqConstants.Key.QA_LIKED_TIMES_KEY
            )
    )
    public void listenLikeTimesChange(LikedTimesDTO dto) {
        //1,判断
        if (dto == null) {
            log.error("dto消息数据有误");
            return;
        }
        //2.转成reply对象
        InteractionReply reply = new InteractionReply();
        reply.setId(dto.getBizId());
        reply.setLikedTimes(dto.getLikedTimes());
        //3,根据id该更新
        replyService.updateById(reply);
    }
}
